package com.koron.order.maintain.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.maintain.bean.entity.MaintainItemPo;
import com.koron.order.maintain.bean.vo.MaintainItemVo;
import com.koron.order.maintain.mapper.MaintainItemMapper;
import com.koron.order.maintain.service.MaintainItemService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * (MaintainItem)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-23 15:53:22
 */
@Service("maintainItemService")
public class MaintainItemServiceImpl implements MaintainItemService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, MaintainItemPo maintainItem) {

        MaintainItemMapper mapper = factory.getMapper(MaintainItemMapper.class);
        return mapper.insert(maintainItem);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<MaintainItemPo> maintainItemPos) {

        if (CollectionUtils.isEmpty(maintainItemPos)) {
            return 0;
        }
        MaintainItemMapper mapper = factory.getMapper(MaintainItemMapper.class);
        maintainItemPos.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });
        mapper.deleteByMaintainId(maintainItemPos.get(0).getMaintainId());
        return mapper.insertBatch(maintainItemPos);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, MaintainItemPo maintainItem) {
        MaintainItemMapper mapper = factory.getMapper(MaintainItemMapper.class);
        return mapper.update(maintainItem);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        MaintainItemMapper mapper = factory.getMapper(MaintainItemMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public MaintainItemPo queryById(SessionFactory factory, String id) {

        MaintainItemMapper mapper = factory.getMapper(MaintainItemMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<MaintainItemVo> queryByEquipmentId(SessionFactory factory, String orderId, String maintainEquipmentId) {

        MaintainItemMapper mapper = factory.getMapper(MaintainItemMapper.class);

        List<MaintainItemVo> itemVos = mapper.queryByEquipmentId(orderId, maintainEquipmentId);

        if (CollectionUtils.isNotEmpty(itemVos)) {
            Map<String, MaintainItemVo> map = new HashMap<>();
            for (MaintainItemVo itemVo : itemVos) {
                String id = itemVo.getId();
                String parentId = itemVo.getParentId();
                itemVo.setMaintainItemVoList(new ArrayList<>());
                if (StringUtils.isEmpty(parentId)) {
                    map.put(id, itemVo);
                }
            }
            Iterator<MaintainItemVo> iterator = itemVos.iterator();

            while (iterator.hasNext()) {
                MaintainItemVo itemVo = iterator.next();
                String id = itemVo.getId();
                String parentId = itemVo.getParentId();
                if (StringUtils.isNotEmpty(parentId)) {
                    MaintainItemVo maintainItemVo = map.get(parentId);
                    if (!Objects.isNull(maintainItemVo)) {
                        //找到了父节点,应该从原来列表删除
                        iterator.remove();
                        List<MaintainItemVo> maintainItemVoList = maintainItemVo.getMaintainItemVoList();
                        maintainItemVoList.add(itemVo);
                    }
                }
            }

        }

        return itemVos;
    }

    @Override
    @TaskAnnotation("queryList")
    public List<MaintainItemPo> queryList(SessionFactory factory, MaintainItemPo maintainItem) {

        MaintainItemMapper mapper = factory.getMapper(MaintainItemMapper.class);

        List<MaintainItemPo> list = mapper.queryList(maintainItem);

        return list;
    }

}